﻿#nullable enable

namespace NServiceBus;

using Transport;
using System;
using System.Threading.Tasks;
using Pipeline;

sealed class ThrowIfCannotDeferMessageBehavior : IBehavior<IRoutingContext, IRoutingContext>
{
    public Task Invoke(IRoutingContext context, Func<IRoutingContext, Task> next)
    {
        if (context.Extensions.TryGet<DispatchProperties>(out var properties) && (properties.DelayDeliveryWith != null || properties.DoNotDeliverBefore != null))
        {
            throw new InvalidOperationException("Cannot delay delivery of messages when there is no infrastructure support for delayed messages.");
        }

        return next(context);
    }
}